﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Altairis.MailToolkit.ListManager.Pages {
    public partial class Verify : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {
            var listName = this.RouteData.Values["list"] as string ;
            var code = this.RouteData.Values["code"] as string;
            var email = this.RouteData.Values["email"] as string;
            var emailAddress = new MailAddress(email);

            if ("s".Equals(this.RouteData.Values["operation"])) {
                // Subscribe
                var r = MailingListManager.SubscribeConfirm(listName, emailAddress, code);
                if (r) {
                    this.LiteralMessage.Text = string.Format(Resources.UI.MsgSubSuccess, email, listName);
                }
                else {
                    this.LiteralMessage.Text = Resources.UI.MsgSubFailed;
                }
            }
            else if ("r".Equals(this.RouteData.Values["operation"])) {
                // Remove
                var r = MailingListManager.SubscribeConfirm(listName, emailAddress, code);
                if (r) {
                    this.LiteralMessage.Text = string.Format(Resources.UI.MsgRemSuccess, email, listName);
                }
                else {
                    this.LiteralMessage.Text = Resources.UI.MsgRemFailed;
                }
            }
        }

        protected void BackButton_Click(object sender, EventArgs e) {
            Response.RedirectToRoute("HomePage");
        }

    }
}